package dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import model.impl.DisturbancesImpl;

import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.resotc.dao.DisturbancesDAO;
import org.resotc.model.Disturbance;

public class DisturbancesDAOImpl extends AbstractXMLDAO implements DisturbancesDAO {
	private final static String indexFileName = "disturbances.xml";
	public HashMap<Integer, Disturbance> disturbances;
	
	public DisturbancesDAOImpl() throws JDOMException, IOException {
		indexFile = new File(rootPath + "/" + indexFileName);

		loadXML();
		parseXML();
	}

	public Disturbance getDisturbance(int id) {
		return disturbances.get(id);
	}

	public HashMap<Integer, Disturbance> getDisturbances() {
		return this.disturbances;
	}

	public void parseXML() {

		Element rootElement = indexXMLInstance.getRootElement();
		Element disturbancesElements = rootElement.getChild("disturbances");
		disturbances = new HashMap<Integer, Disturbance>();

		List<Element> list = (List<Element>) disturbancesElements.getChildren();

		int ID;
		int strenge;
		double to_area;
		double from_area;
		int laneid;

		for (Element element : list) {

			ID = Integer.valueOf(element.getChildText("id"));
			laneid = Integer.valueOf(element.getChildText("Laneid"));
			from_area = Double.valueOf(element.getChild("start").getChildText("area"));
			to_area = Double.valueOf(element.getChild("end").getChildText("area"));
			strenge = Integer.valueOf(element.getChildText("strenge"));
			Disturbance disturbancesImpl = new DisturbancesImpl(ID, from_area, to_area, laneid, strenge);
			disturbances.put(ID, disturbancesImpl);
		}
	}

}
